<template>
  <div class="info" v-loading="loading">
    <div class="card_con">
      <div class="row mb15">
        <span class="l" @click="handleAdd">
          <i class="el-icon-circle-plus-outline"></i>新增
        </span>
      </div>
      <el-table
        border
        :data="tableData"
        :header-cell-style="{
          'text-align': 'center',
        }"
        :cell-style="{ 'text-align': 'center' }"
        style="width: 100%"
        highlight-current-row
      >
        <el-table-column prop="mch" label="商家id" min-width="15%">
        </el-table-column>
        <el-table-column prop="mch" label="商家名称" min-width="20%">
        </el-table-column>
        <el-table-column prop="rate" label="比例" min-width="20%">
        </el-table-column>
        <el-table-column prop="area_name" label="区域" min-width="20%">
        </el-table-column>
        <el-table-column label="操作" min-width="30%">
          <template slot-scope="scope">
            <div class="caozuo">
              <div
                class="handleDetail hand_cs"
                @click="handleDetail(scope.row)"
              >
                详情
              </div>

              <div
                class="handleDelete hand_cs"
                @click="handleDelete(scope.row)"
              >
                删除
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <Pagination :page="page" @onCurrentChange="handleCurrentChange" />
      </div>
      <el-dialog
        class="detailDialog"
        :title="title"
        :visible.sync="addEditDialog"
        width="40%"
      >
        <Dialog v-if="addEditDialog" @handleClose="handleClose"></Dialog>
      </el-dialog>
      <el-dialog
        class="detailDialog"
        title="详情"
        :visible.sync="detailDialog"
        width="60%"
      >
        <DetailDialog
          v-if="detailDialog"
          @handleClose="handleClose"
          @close="close"
          :rowData="rowData"
        ></DetailDialog>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination.vue";
import Dialog from "./addEdit.vue";
import DetailDialog from "./detail.vue";
import { agentList, agentDel } from "@/api/merchant";
export default {
  components: { Pagination, Dialog, DetailDialog },
  data() {
    return {
      time: "",
      page: {
        total: 0,
        pageNum: 1, // 当前页数
        pageSize: 10, // 每页的条数
      },
      loading: false,
      tableData: [{}],
      addEditDialog: false,
      detailDialog: false,
      title: "",
      rowData: {},
    };
  },
  methods: {
    async agentList() {
      const { data, err } = await agentList({
        page: this.page.pageNum,
      });
      if (err === 0) {
        this.tableData = data.data;
        this.page.total = data.total;
        this.page.per_page = data.per_page;
      }
    },
    async agentDel(id) {
      const { msg, err } = await agentDel({
        mch: id,
      });
      if (err === 0) {
        this.$message({
          showClose: true,
          message: "操作成功",
          type: "success",
        });
        this.agentList();
      } else {
        this.$message({
          showClose: true,
          message: msg,
          type: "error",
        });
      }
    },
    handleAdd() {
      this.title = "添加";
      this.addEditDialog = true;
    },
    //详情
    handleDetail(row) {
      this.detailDialog = true;
      this.rowData = row;
    },
    handleClose() {
      this.addEditDialog = false;
      this.detailDialog = false;
      this.agentList();
    },
    //删除
    handleDelete(row) {
      this.$confirm("是否确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.agentDel(row.mch);
        })
        .catch(() => {});
    },
    // 当前页: ${val}
    handleCurrentChange(val) {
      this.loading = true;
      this.page.pageNum = val;
    },
    close() {
      this.addEditDialog = false;
      this.agentList();
    },
  },
  created() {
    this.agentList();
  },
};
</script>

<style lang="scss" scoped>
@import "@/style/index.scss";
.handleExamine {
  margin-left: 20px;
}
.info {
  .title {
    font-size: 16px;
    margin-bottom: 13px;
  }
}
.pagination {
  display: flex;
  justify-content: flex-end;
  padding-top: 10px;
}
.el-select {
  width: 100%;
}

.tb {
  background-color: #fff;
  padding: 20px;
  box-sizing: border-box;
}
.row {
  display: flex;
  border-radius: 2px;
  .l {
    padding: 8px 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    border: 1px solid #ccc;
    font-size: 13px;
    i {
      margin-right: 6px;
      font-size: 13px;
    }
    &:hover {
      cursor: pointer;
    }
  }
  .l:nth-child(3) {
    border-right: 0;
  }
}
.caozuo {
  display: flex;
  justify-content: center;
  .handleDelete {
    margin-left: 20px;
  }
}
.detailDialog {
  /deep/.el-dialog {
    height: 82%;
    margin-top: 10vh !important;
    .el-dialog__body {
      height: 80%;
      overflow: auto;
    }
  }
}
.handleEdit {
  margin-left: 20px;
}
.handleDetail {
  color: rgb(21, 204, 54);
}
</style>
